package com.controller;

import com.entity.Artical;
import com.entity.ArticalComment;
import com.service.ArticalCommenServie;
import com.service.impl.ArticalCommentServiceImpl;
import com.util.json.Result;
import com.util.servlet.BaseController;
import com.util.sql.PageModel;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 文章评论接口
 */
@WebServlet("/ArticalComment")
public class ArticalCommentController extends BaseController {
    private ArticalCommenServie artical = new ArticalCommentServiceImpl();
    private HttpServletRequest request;
    private HttpSession session;

    /**
     * 发送评论
     * @param ar
     * @return
     */
    public String sendComment(ArticalComment ar){

        try {
//            request = this.getRequest();
//            session = request.getSession();
//            System.out.println(session.getAttribute("name"));
            ArticalComment ac = artical.sendComment(ar);
            return ac == null  ? Result.fial("发送失败") : Result.successObj(ac);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }
    }

    /**
     * 修改评论
     * @param ar
     * @return
     */
    public String updateComment(ArticalComment ar){
        try {
            int ac = artical.updateComment(ar);
            return ac<=0 ? Result.fial("修改失败") : Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }
    }

    /**
     * 删除评论
     * @param ar
     * @return
     */
    public String delComment(ArticalComment ar){
        try {
            int i = artical.deleteComment(ar);
            return i <= 0 ? Result.fial("删除失败") : Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }
    }

    /**
     * 查询评论
     * @param ar
     * @return
     */
    public String queryComment(ArticalComment ar){
        try {
            ArticalComment ac = artical.queryComment(ar);
            return ac==null ? Result.fial("查询评论失败") : Result.successObj(ac);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }
    }

    /**
     * 帖子评论分页查询
     * @param page
     * @param size
     * @param ar
     * @return
     */
    public String queryPage(int page,int size,ArticalComment ar){

        try {
            PageModel ac = artical.queryPage(page,size,ar);
            return ac==null ? Result.fial("分页失败") : Result.successModelObj(ac);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fial(e.getMessage());
        }
    }

}
